//
//  XBirthdayAlertView.m
//  ReceiveVouchers
//
//  Created by Veer on 2017/11/16.
//  Copyright © 2017年 ReceiveVouchers. All rights reserved.
//

#import "XBirthdayAlertView.h"


@interface XBirthdayAlertView()

@property (nonatomic, weak)  UIDatePicker *datePicker;

@end

@implementation XBirthdayAlertView

- (instancetype)init
{
    
    if (self = [super initWithTitle:@"请选择您的生日" sureBtnTitle:@"保存" cancleBtnTitle:@"取消" size:CGSizeMake(280, 300)]) {
        
        WeakSelf(weakSelf)
        self.ActionBlock = ^(NSInteger index) {
            
            if (index == 1) {
                
                NSDate *date = weakSelf.datePicker.date;
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                formatter.dateFormat = @"yyyy-MM-dd";
                NSString *dateStr = [formatter stringFromDate:date];
                if (weakSelf.SaveBlock) {
                    weakSelf.SaveBlock(dateStr);
                }
            }
        };
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [self.alertView addSubview:datePicker];
    self.datePicker = datePicker;
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.alertView);
        make.top.mas_equalTo(60);
        make.width.mas_equalTo(280);
    }];
}

#pragma mark - setter && getter
- (void)setDefaultDateStr:(NSString *)defaultDateStr
{
    _defaultDateStr = defaultDateStr;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate *date = [formatter dateFromString:defaultDateStr];
    
    self.datePicker.date = date;
}

@end
